package com.mocai.merchant.core.domain;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/20.
 */
public class Wrapper extends LinkedHashMap {

    public Wrapper() {
    }

    public Wrapper(Map map) {
        super(map);
    }

    private <T> T get(Object key, Class<T> type) {
        Object value = super.get(key);
        if (null == value) {
            return null;
        }
        // 如果是数组，获取数组的第一个值
        if (value.getClass().isArray()) {
            value = Array.get(value, 0);
        }
        if (CharSequence.class.isAssignableFrom(type)) {
            return (T) value.toString();
        }
        if (BigDecimal.class.isAssignableFrom(type)) {
            return (T) BigDecimal.valueOf(Double.valueOf(value.toString()));
        }
        if (Double.class.isAssignableFrom(type)) {
            return (T) Double.valueOf(value.toString());
        }
        if (Float.class.isAssignableFrom(type)) {
            return (T) Float.valueOf(value.toString());
        }
        if (Long.class.isAssignableFrom(type)) {
            return (T) Long.valueOf(value.toString());
        }
        if (Integer.class.isAssignableFrom(type)) {
            return (T) Integer.valueOf(value.toString());
        }
        if (Short.class.isAssignableFrom(type)) {
            return (T) Short.valueOf(value.toString());
        }
        if (Byte.class.isAssignableFrom(type)) {
            return (T) Byte.valueOf(value.toString());
        }
        throw new IllegalArgumentException("不支持的值类型");
    }

    public String getString(Object key) {
        return get(key, String.class);
    }

    public Double getDouble(Object key) {
        return get(key, Double.class);
    }

    public Float getFloat(Object key) {
        return get(key, Float.class);
    }

    public Long getLong(Object key) {
        return get(key, Long.class);
    }

    public Integer getInteger(Object key) {
        return get(key, Integer.class);
    }

    public Short getShort(Object key) {
        return get(key, Short.class);
    }

    public Byte getByte(Object key) {
        return get(key, Byte.class);
    }

}
